package com.lixin.rabbitmq.all;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.3.17");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("123456");
        factory.setVirtualHost("/");
        try {
            // 创建连接connection
            connection = factory.newConnection("adminConn");
            // 创建通道channel
            channel = connection.createChannel();

            // 交换机名称
            String exchangeName = "direct_send_msg_exchange";
            // 交换机类型
            String exchangeType = "direct";
            // 创建交换机
            channel.exchangeDeclare(
                    exchangeName,
                    exchangeType,
                    true // 是否持久化
            );

            // 创建队列
            String queueName1 = "send_msg_queue1";
            channel.queueDeclare(
                    queueName1, // 队列名称
                    true, // 消息是否持久化
                    false, // 是否是独占队列
                    false, // 最后一个消费者消费完消息后，是否将队列删除
                    null // 携带的附加参数
            );
            String queueName2 = "send_msg_queue2";
            channel.queueDeclare(queueName2, true, false, false, null);
            String queueName3 = "send_msg_queue3";
            channel.queueDeclare(queueName3, true, false, false, null);

            // 将队列绑定到交换机上
            channel.queueBind(
                    queueName1, // 队列
                    exchangeName, // 交换机
                    "email" // 路由key
            );
            channel.queueBind(queueName2, exchangeName, "sms");
            channel.queueBind(queueName3, exchangeName, "email");

            // 路由key
            String routingKey = "email";
            // 消息内容
            String message = "发布/订阅模式，带路由KEY的消息";
            // 发送消息给交换机(交换机会将消息投递到所有和其绑定的队列中)
            channel.basicPublish(exchangeName, routingKey, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("消息成功发送！");
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        } finally {
            // 关闭通道
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (IOException | TimeoutException e) {
                    e.printStackTrace();
                }
            }
            // 关闭连接
            if (connection != null && connection.isOpen()) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
